'use strict';
/**
 * Created by win8 on 2015/2/5.
 */
angular.module('htmsMobileApp')
  .controller('EntUserManage', function($scope,$log,$modal,$templateCache,CustomHttp,urlObj2Str,Authentication,$location) {
    $scope.del = function(userId){
      if(userId){
        if(confirm("确定要删除？")){
          CustomHttp.del("/api/entUser/" + userId).success(function(){
            alert("删除成功！");
            var newListUrl = {
              apiUrl : "/api/entUser/list",
              page:1,
              pageSize:5,
              sortType:"userId",
              search : [{
                key:"search_EQ_enterpriseBaseInfo.qyid",value:$scope.qyid
              },{
                key:"search_NEQ_yylx",value:"1"
              }]
            };
            angular.element('#loadingAjaxEntUser').removeAttr("style");
            var u = urlObj2Str.getUrlStr(newListUrl);
            CustomHttp.get(u).success(function (data) {
              $scope.items = data.content;
              $scope.first = data.first;
              $scope.last = data.last;
              //去掉加载中的进度条
              angular.element('#loadingAjaxEntUser').css("display","none");
            })
          });
        }
      }
    }
    var user = Authentication.getUser();
    $scope.qyid = user.depid;
    //设置初始查询的url
    $scope.entsUrl = {
      apiUrl : "/api/entUser/list",
      page:1,
      pageSize:5,
      sortType:"userId",
      search : [{
        key:"search_EQ_enterpriseBaseInfo.qyid",value:$scope.qyid
      },{
        key:"search_NEQ_yylx",value:"1"
      }]
    };
    var u = urlObj2Str.getUrlStr($scope.entsUrl);
    CustomHttp.get(u).success(function (data) {
      $scope.items = data.content;
      $scope.uo = $scope.entsUrl;
      $scope.first = data.first;
      $scope.last = data.last;
      //去掉加载中的进度条
      angular.element('#loadingAjaxEntUser').css("display","none");
    })

    //查询
    $scope.queryEnts = function(){
      angular.element('#loadingAjaxEntUser').removeAttr("style");
      $scope.items = null;
      var uo = {
        apiUrl : "/api/entUser/list",
        page:1,
        pageSize:5,
        sortType:"userId",
        search : [{
          key:"search_EQ_enterpriseBaseInfo.qyid",value:$scope.qyid
        },{
          key:"search_NEQ_yylx",value:"1"
        }]
      };
      if($scope.q.val) {
        uo.search.push({
          key: "search_LIKE_ddm",
          value: $scope.q.val
        })
      }
      var u = urlObj2Str.getUrlStr(uo);
      CustomHttp.get(u).success(function (data) {
        $scope.items = data.content;
        $scope.first = data.first;
        $scope.last = data.last;
        $scope.uo = uo;
        //去掉加载中的进度条
        angular.element('#loadingAjaxEntUser').css("display","none");
      })

    };
    //下一页
    $scope.nextPage = function (){
      var uo;
      if($scope.uo)
        uo = $scope.uo;
      else uo = $scope.entsUrl;
      $scope.items = null;
      angular.element('#loadingAjaxEntUser').removeAttr("style");
      uo.page ++;
      var u = urlObj2Str.getUrlStr(uo);
      CustomHttp.get(u).success(function (data) {
        $scope.items = data.content;
        $scope.first = data.first;
        $scope.last = data.last;
        $scope.uo = uo;
        //去掉加载中的进度条
        angular.element('#loadingAjaxEntUser').css("display","none");
      })
    };
    //上一页
    $scope.prePage = function (){
      var uo;
      if($scope.uo)
        uo = $scope.uo;
      else uo = $scope.entsUrl;
      $scope.items = null;
      angular.element('#loadingAjaxEntUser').removeAttr("style");
      uo.page --;
      var u = urlObj2Str.getUrlStr(uo);
      CustomHttp.get(u).success(function (data) {
        $scope.items = data.content;
        $scope.first = data.first;
        $scope.last = data.last;
        $scope.uo = uo;
        //去掉加载中的进度条
        angular.element('#loadingAjaxEntUser').css("display","none");
      })
    };
  })
  .controller('EntUserDetail', function($scope,CustomHttp,$routeParams,Authentication,$location){

    var id = $routeParams.userId;
    var now = new Date();
    var year = now.getFullYear();
    var yearList = [];
    for(var i = 0;i <= 70;i++){
      yearList.push(year-i);
    }
    $scope.yearList = yearList;
    if(id == 0){
      $scope.flag = 1;
      $scope.editOrAdd = "新增";
      $scope.userInfo = {
        "yyzt": 0,
        "cscs": 0,
        "sbsjjl": "2015-01-01",
        "yylx": 2,
        "enterpriseBaseInfo": {
        "qyid": Authentication.getUser().depid
      }
      }
    }else {
      $scope.editOrAdd = "修改";
      $scope.flag = 0;
      CustomHttp.get('/api/entUser/' + id).success(function (data) {
        $scope.userInfo = data;
        if($scope.userInfo.birthday){
          var date = $scope.userInfo.birthday.split("-");
          $scope.birjnf = date[0];
          $scope.birjyf = date[1];
          $scope.birjday = date[2];
        }
        $scope.qrmima = data.mima;
      })
    }
    $scope.edit = function(){
      $scope.flag = 1;
    }
    var match = function(rq){
      return /\d{4}-\d{2}-\d{2}/.test(rq);
    };

    $scope.checkName = function(){
      if($scope.userInfo.yym){
        CustomHttp.get('/api/entUser/exists/'+ $scope.userInfo.yym).success(function (data) {
          if(data.exists == false){
            $scope.entUserModelForm1.yym.$valid = false;
          }else {
            $scope.entUserModelForm1.yym.$valid = true;
          }
        });
      }
    }

    $scope.updata = function(){
      if($scope.flag == 0){
        return;
      }
      if($scope.editOrAdd == "新增"){
        if(!$scope.userInfo.mima || !$scope.qrmima){
          alert("登录密码和确认密码都不能为空！");
          return;
        }
        if($scope.qrmima != $scope.userInfo.mima){
          alert("两次输入密码不一致！");
          return;
        }
        //$scope.userInfo.enterpriseBaseInfo = {qyid:Authentication.getUser().depid};
        $scope.userInfo.birthday = $scope.birjnf + "-" + $scope.birjyf + "-"+$scope.birjday;
        if(!match($scope.userInfo.birthday)){
          $scope.userInfo.birthday = null;
        }
        //$scope.userInfo.yylx = 1;
        //$scope.userInfo.cscs = 0;
        console.log($scope.userInfo);
        CustomHttp.post('/api/entUser/create',$scope.userInfo).success(function (data) {
          console.log(data);
          alert("新增成功");
          $location.path("/entUserList");
        })
      }else {
        $scope.userInfo.birthday = $scope.birjnf + "-" + $scope.birjyf + "-"+$scope.birjday;
        if(!match($scope.userInfo.birthday)){
          $scope.userInfo.birthday = null;
        }
        $scope.userInfo.enterpriseBaseInfo = {"qyid":Authentication.getUser().depid};
        CustomHttp.put('/api/entUser/' + id,$scope.userInfo).success(function (data) {
          console.log(data);
          alert("修改成功");
        })
      }
    }
  });
